package com.yun.zhong.fu.usercenter.expection;

import java.lang.SecurityException;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author RenPu
 * @Date 2021/1/5 16:32
 * @Version 1.0
 * @Description: 利用spring mvc的全局异常 更改页面异常展示效果，增加用户体验
 **/
@RestControllerAdvice(basePackages = "com.yun.zhong.fu.usercenter.controller")
@Slf4j
public class GloableException {

    @ExceptionHandler(SecurityException.class)
    public ResponseEntity<ErrorBody> error(SecurityException e) {
        log.info("发生了SecrityExpection异常", e);
        return new ResponseEntity<>(
                ErrorBody.builder()
                        .body(e.getMessage())
                        .status(HttpStatus.UNAUTHORIZED.value())
                        .build(),
                HttpStatus.UNAUTHORIZED);
    }
}


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
class ErrorBody {
    private String body;
    private Integer status;
}

